Imports System.Data.SqlClient

Public Class cls_recibo_

    Implements ICloneable

#Region "Variables ..."
    Private int_idrecibo As Integer = 0
    Private dte_fecha As DateTime = New System.DateTime(Now.Year, 1, 1)
    Private int_idtercero As Integer = 0
    Private int_idtalonario As Integer = 0
    Private int_numero As Integer = 0
    Private dbl_importe As Double = 0
    Private str_observaciones As String = ""
    Private int_idregistro As Integer = 0
    Private int_idcajaasiento As Integer = 0

    Private obj_registro As cls_registro_ = Nothing
    Private obj_tercero As cls_tercero_ = Nothing
    Private obj_talonariorecibo As cls_talonariorecibo_ = Nothing
    Private obj_reciboimputacion_ As cls_reciboimputacion_ = Nothing
    Private obj_cajaasiento As cls_cajaasiento_ = Nothing
    Private col_Items As Collections.Generic.List(Of cls_recibo_) = Nothing
#End Region

    Public Overrides Function ToString() As String
        Return fecha & " Recibo: " & numero & " " & tercero.ToString
    End Function

#Region "Propiedades ..."
    Property idrecibo() As Integer
        Get
            idrecibo = int_idrecibo
        End Get
        Set(ByVal value As Integer)
            int_idrecibo = value
        End Set
    End Property
    Property fecha() As DateTime
        Get
            fecha = dte_fecha
        End Get
        Set(ByVal value As DateTime)
            dte_fecha = value
        End Set
    End Property
    Property idtercero() As Integer
        Get
            idtercero = int_idtercero
        End Get
        Set(ByVal value As Integer)
            int_idtercero = value
        End Set
    End Property
    Property idtalonario() As Integer
        Get
            idtalonario = int_idtalonario
        End Get
        Set(ByVal value As Integer)
            int_idtalonario = value
        End Set
    End Property
    Property numero() As Integer
        Get
            numero = int_numero
        End Get
        Set(ByVal value As Integer)
            int_numero = value
        End Set
    End Property
    Property importe() As Double
        Get
            importe = dbl_importe
        End Get
        Set(ByVal value As Double)
            dbl_importe = value
        End Set
    End Property
    Property observaciones() As String
        Get
            observaciones = str_observaciones
        End Get
        Set(ByVal value As String)
            str_observaciones = value
        End Set
    End Property
    Property idregistro() As Integer
        Get
            idregistro = int_idregistro
        End Get
        Set(ByVal value As Integer)
            int_idregistro = value
        End Set
    End Property
    Property registro() As cls_registro_
        Get
            If IsNothing(obj_registro) Then
                obj_registro = New cls_registro_
                If Not obj_registro.Buscar(idregistro) Then
                    obj_registro = Nothing
                End If
            End If
            registro = obj_registro
        End Get
        Set(ByVal value As cls_registro_)
            obj_registro = value
        End Set
    End Property
    Property cajaasiento() As cls_cajaasiento_
        Get
            If IsNothing(obj_cajaasiento) Then
                obj_cajaasiento = New cls_cajaasiento_
                If Not obj_cajaasiento.Buscar(idcajaasiento) Then
                    obj_cajaasiento = Nothing
                End If
            End If
            cajaasiento = obj_cajaasiento
        End Get
        Set(ByVal value As cls_cajaasiento_)
            obj_cajaasiento = value
        End Set
    End Property
    Property idcajaasiento As Integer
        Get
            idcajaasiento = int_idcajaasiento
        End Get
        Set(ByVal value As Integer)
            int_idcajaasiento = value
        End Set
    End Property
    Property reciboimputacion_() As cls_reciboimputacion_
        Get
            If IsNothing(obj_reciboimputacion_) Then
                obj_reciboimputacion_ = New cls_reciboimputacion_
                If Not obj_reciboimputacion_.CargarXidrecibo(idrecibo) Then
                    obj_reciboimputacion_ = Nothing
                End If
            End If
            reciboimputacion_ = obj_reciboimputacion_
        End Get
        Set(ByVal value As cls_reciboimputacion_)
            obj_reciboimputacion_ = value
        End Set
    End Property
    Property talonariorecibo() As cls_talonariorecibo_
        Get
            If IsNothing(obj_talonariorecibo) Then
                obj_talonariorecibo = New cls_talonariorecibo_
                If Not obj_talonariorecibo.Buscar(idtalonario) Then
                    obj_talonariorecibo = Nothing
                End If
            End If
            talonariorecibo = obj_talonariorecibo
        End Get
        Set(ByVal value As cls_talonariorecibo_)
            obj_talonariorecibo = value
        End Set
    End Property
    Property tercero() As cls_tercero_
        Get
            If IsNothing(obj_tercero) Then
                obj_tercero = New cls_tercero_
                If Not obj_tercero.Buscar(idtercero) Then
                    obj_tercero = Nothing
                End If
            End If
            tercero = obj_tercero
        End Get
        Set(ByVal value As cls_tercero_)
            obj_tercero = value
        End Set
    End Property
    Property Items() As Collections.Generic.List(Of cls_recibo_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_recibo_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_recibo_)
        While obj_Reader.Read()
            Dim Elemento As New cls_recibo_
            Elemento.int_idrecibo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idrecibo")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("fecha"))) = False Then
                Elemento.dte_fecha = (obj_Reader.GetValue(obj_Reader.GetOrdinal("fecha")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idtercero"))) = False Then
                Elemento.int_idtercero = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idtercero")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idtalonario"))) = False Then
                Elemento.int_idtalonario = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idtalonario")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("numero"))) = False Then
                Elemento.int_numero = (obj_Reader.GetValue(obj_Reader.GetOrdinal("numero")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("importe"))) = False Then
                Elemento.dbl_importe = (obj_Reader.GetValue(obj_Reader.GetOrdinal("importe")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("observaciones"))) = False Then
                Elemento.str_observaciones = (obj_Reader.GetValue(obj_Reader.GetOrdinal("observaciones")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro"))) = False Then
                Elemento.int_idregistro = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idcajaasiento"))) = False Then
                Elemento.int_idcajaasiento = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idcajaasiento")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        int_idrecibo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idrecibo")))

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("fecha"))) = False Then
            dte_fecha = (obj_Reader.GetValue(obj_Reader.GetOrdinal("fecha")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idtercero"))) = False Then
            int_idtercero = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idtercero")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idtalonario"))) = False Then
            int_idtalonario = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idtalonario")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("numero"))) = False Then
            int_numero = (obj_Reader.GetValue(obj_Reader.GetOrdinal("numero")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("importe"))) = False Then
            dbl_importe = (obj_Reader.GetValue(obj_Reader.GetOrdinal("importe")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("observaciones"))) = False Then
            str_observaciones = (obj_Reader.GetValue(obj_Reader.GetOrdinal("observaciones")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro"))) = False Then
            int_idregistro = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idcajaasiento"))) = False Then
            int_idcajaasiento = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idcajaasiento")))
        End If
    End Sub

    Public Sub Nuevo(Optional ByVal ObtenerID As Boolean = False)
        Dim obj_Conexion As New cls_Conexion
        Try
            'Creacion de registro----------------------------------------------------
            If IsNothing(obj_registro) Then
                registro = New cls_registro_
                registro.fechacreado = Now
                registro.idusuariocreado = UsuarioLOG.idusuario
                registro.fechamodificado = Now
                registro.idusuariomodificado = UsuarioLOG.idusuario
                registro.idregistroestado = 1
                registro.idusuarioregistroestado = UsuarioLOG.idusuario
                registro.Nuevo(True)
                idregistro = obj_registro.idregistro
            End If
            '------------------------------------------------------------------------

            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "recibo__Nuevo"
                .Parameters.Add("@fecha", SqlDbType.DateTime).Value = dte_fecha
                .Parameters.Add("@idtercero", SqlDbType.Int).Value = int_idtercero
                .Parameters.Add("@idtalonario", SqlDbType.Int).Value = int_idtalonario
                .Parameters.Add("@numero", SqlDbType.Int).Value = int_numero
                .Parameters.Add("@importe", SqlDbType.Decimal).Value = dbl_importe
                .Parameters.Add("@observaciones", SqlDbType.NVarChar).Value = str_observaciones
                .Parameters.Add("@idregistro", SqlDbType.Int).Value = int_idregistro
                .Parameters.Add("@idcajaasiento", SqlDbType.Int).Value = int_idcajaasiento
                .ExecuteNonQuery()
            End With
            CargarUltimo()
            'Creacion de reciboimputacion_--------------------------------------------------
            If Not IsNothing(reciboimputacion_) Then
                If Not IsNothing(reciboimputacion_.Items) Then
                    For Each reciboimputacion As cls_reciboimputacion_ In reciboimputacion_.Items
                        reciboimputacion.idrecibo = idrecibo
                        reciboimputacion.Nuevo(True)
                    Next
                End If
            End If
            '-------------------------------------------------------------------------
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Crear el registro de recibo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try
            'Modificacion de registro------------------------------------------------
            registro.fechamodificado = Now : registro.idusuariomodificado = UsuarioLOG.idusuario
            registro.Modificar()
            '------------------------------------------------------------------------

            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "recibo__Modificar"
                .Parameters.Add("@idrecibo", SqlDbType.Int).Value = int_idrecibo
                .Parameters.Add("@fecha", SqlDbType.DateTime).Value = dte_fecha
                .Parameters.Add("@idtercero", SqlDbType.Int).Value = int_idtercero
                .Parameters.Add("@idtalonario", SqlDbType.Int).Value = int_idtalonario
                .Parameters.Add("@numero", SqlDbType.Int).Value = int_numero
                .Parameters.Add("@importe", SqlDbType.Decimal).Value = dbl_importe
                .Parameters.Add("@observaciones", SqlDbType.NVarChar).Value = str_observaciones
                .Parameters.Add("@idregistro", SqlDbType.Int).Value = int_idregistro
                .Parameters.Add("@idcajaasiento", SqlDbType.Int).Value = int_idcajaasiento
                .ExecuteNonQuery()
            End With

            'Creacion/modificacion de produccionresultado-------------------------------------
            If Not IsNothing(reciboimputacion_) Then
                If Not IsNothing(reciboimputacion_.Items) Then
                    For Each reciboimputacion As cls_reciboimputacion_ In reciboimputacion_.Items
                        If reciboimputacion.idreciboimputacion = 0 Then
                            reciboimputacion.idrecibo = idrecibo
                            reciboimputacion.Nuevo(True)
                        Else
                            reciboimputacion.Modificar()
                        End If
                    Next
                End If
            End If
            '-------------------------------------------------------------------------
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Modificar el registro de recibo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal idrecibo As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "recibo__Eliminar"
                .Parameters.Add("@idrecibo", SqlDbType.Int).Value = idrecibo
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Eliminar el registro de recibo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal idrecibo As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "recibo__Buscar"
                .Parameters.Add("@idrecibo", SqlDbType.Int).Value = idrecibo
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Buscar el registro de recibo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "recibo__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarUltimo registro de recibo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos(Optional ByVal idregistroestado As Integer = 1) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "recibo__CargarTodos"
                .Parameters.Add("@idregistroestado", SqlDbType.Int).Value = idregistroestado
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarTodos los registros de recibo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_recibo_Clon As New cls_recibo_
        obj_recibo_Clon.int_idrecibo = Me.int_idrecibo
        obj_recibo_Clon.dte_fecha = Me.dte_fecha
        obj_recibo_Clon.int_idtercero = Me.int_idtercero
        obj_recibo_Clon.int_idtalonario = Me.int_idtalonario
        obj_recibo_Clon.int_numero = Me.int_numero
        obj_recibo_Clon.dbl_importe = Me.dbl_importe
        obj_recibo_Clon.str_observaciones = Me.str_observaciones
        obj_recibo_Clon.int_idregistro = Me.int_idregistro
        obj_recibo_Clon.int_idcajaasiento = Me.int_idcajaasiento
        obj_recibo_Clon.col_Items = Me.col_Items
        Return obj_recibo_Clon
    End Function

End Class

